package com.qztc.news_end_core.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.qztc.news_end_core.util.MD5Parse;
import com.qztc.news_end_dto.dto.Admin;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/admin")
@Tag(name = "管理员接口")
public class AdminController {

    @Operation(summary = "管理员登录")
    @PostMapping("/login")
    public long login(@RequestBody Admin admin) {
        admin.setAdminpwd(MD5Parse.encryptMD5(admin.getAdminpwd()));
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("adminname", admin.getAdminname());

        queryWrapper.eq("adminpwd", admin.getAdminpwd());
        return new Admin().selectCount(queryWrapper);
    }

    @Operation(summary = "删除管理员")
    @DeleteMapping("/delete")
    public boolean delete(@RequestBody Admin admin) {
        return admin.deleteById();
    }

    @Operation(summary = "添加管理员")
    @PostMapping("/add")
    public boolean add(@RequestBody Admin admin) {
        admin.setAdminpwd(MD5Parse.encryptMD5(admin.getAdminpwd()));
        return admin.insert();
    }

    @Operation(summary = "获取所有管理员")
    @GetMapping("/get")
    public List<Admin> get(){
        return new Admin().selectAll();
    }

    @Operation(summary = "修改管理员信息")
    @PutMapping("/update")
    public boolean update(@RequestBody Admin admin) {
        admin.setAdminpwd(MD5Parse.encryptMD5(admin.getAdminpwd()));
        return admin.updateById();
    }
}
